<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MessageController extends BaseController
{
    public function index()
    {
        $user_id = auth('api')->id();
        $messages = DB::table('user_messages')
                    ->selectRaw('type,count(id) as total_count,
                    (select count(id) from user_messages where is_read=1 and type=user_messages.type and user_id='.$user_id.') as count,
                    (select content from user_messages where is_read=1 and type=user_messages.type and user_id='.$user_id.' limit 1) as content,
                    (select created_at from user_messages where is_read=1 and type=user_messages.type and user_id='.$user_id.' limit 1) as created_at'
                    )
                    ->groupBy('type')
                    ->orderBy('type')
                    ->where('user_id',$user_id)
                    ->get();

        if ($messages->isEmpty()){
            return  $this->success();
        }

        foreach ($messages as &$message){
            $count = DB::table('user_messages')
                        ->where('type',$message->type)
                        ->where('user_id',$user_id)
                        ->where('is_read',1)
                        ->count('id');
            $message->count = $count;
            $content = DB::table('user_messages')
                        ->where('type',$message->type)
                        ->where('user_id',$user_id)
                         ->orderByDesc('id')
                            ->value('content');
            $message->content  = $content ;
            $created_at = DB::table('user_messages')
                            ->where('type',$message->type)
                            ->where('user_id',$user_id)
                            ->orderByDesc('id')
                             ->value('created_at');
            $message->created_at  = $created_at ;
        }

        return $this->success($messages);
    }


    public function read(Request $request)
    {
        $param = $this->validate($request,[
            'type' => 'required|in:1,2,3,4',
        ]);

        $result =  DB::table('user_messages')
                    ->where('user_id',auth('api')->id())
                    ->where($param)
                    ->update([
                        'is_read' => 2
                     ]);

        return $result ? $this->success() : $this->error();
    }

    public function list(Request $request)
    {
        $param = $this->validate($request,[
            'type' => 'required|in:1,2,3,4',
        ]);

        $messages = DB::table('user_messages')
                      ->where($param)
                      ->where('user_id',auth('api')->id())
                      ->orderByDesc('id')
                      ->simplePaginate();

        return $this->success($messages);
    }

    public function show(Request $request)
    {
        $param = $this->validate($request,[
            'message_id' => 'required|integer',
        ]);

        $message = DB::table('user_messages')
            ->where('id',$param['message_id'])
            ->first();

        DB::table('user_messages')->where('id',$param['message_id'])->update(['is_read' =>2]);

        return $this->success($message);
    }
}
